package com.icode.web.validator;

import com.icode.core.dto.house.HomePageDTO;
import com.icode.core.model.house.HomeInfoImage;
import com.icode.core.model.house.HomeIntroImage;
import com.icode.core.model.house.HomePartnerImage;
import com.icode.service.house.HomePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-3-4
 * Time: 上午10:08
 * To change this template use File | Settings | File Templates.
 */
@Component
public class HomePageFormValidator implements Validator {

    @Autowired
    private HomePageService homePageService;

    @Override
    public boolean supports(Class<?> clazz) {
        return HomePageDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        HomePageDTO homePageDTO = (HomePageDTO) target;
        String title = homePageDTO.getTitle();
        String contents = homePageDTO.getContents();
        List<MultipartFile> introImg = homePageDTO.getIntroImg();
        List<MultipartFile> infoImg = homePageDTO.getInfoImg();
        List<MultipartFile> partnersImg = homePageDTO.getPartnersImg();
        if (!"".equals(homePageDTO.getId())) {
            List<HomeIntroImage> homeIntroImages = homePageService.findHomeIntroImageByHomePageId(homePageDTO.getId());
            List<HomeInfoImage> homeInfoImages = homePageService.findHomeInfoImageByHomePageId(homePageDTO.getId());
            List<HomePartnerImage> homePartnerImages = homePageService.findHomePartnerImageByHomePageId(homePageDTO.getId());
            homePageDTO.setIntroImgGuids(HomePageDTO.initIntroImgGuids(homeIntroImages));
            homePageDTO.setInfoImgGuids(HomePageDTO.initInfoImgGuids(homeInfoImages));
            homePageDTO.setPartnersImgGuids(HomePageDTO.initPartnerImgGuids(homePartnerImages));
        }
        if (isNull(title) || isNull(contents)) {
            return;
        }
        if (!imgIsNull(introImg)) {
            if (!isImages(introImg)) {
                errors.rejectValue("introImg", null, "图片格式不正确!");
            }
            if (isLarge(introImg)) {
                errors.rejectValue("introImg", null, "图片不能超过5M!");
            }
        }
        if (!imgIsNull(infoImg)) {
            if (!isImages(infoImg)) {
                errors.rejectValue("infoImg", null, "图片格式不正确!");
            }
            if (isLarge(infoImg)) {
                errors.rejectValue("infoImg", null, "图片不能超过5M!");
            }
        }
        if (!imgIsNull(partnersImg)) {
            if (!isImages(partnersImg)) {
                errors.rejectValue("partnersImg", null, "图片格式不正确!");
            }
            if (isLarge(partnersImg)) {
                errors.rejectValue("partnersImg", null, "图片不能超过5M!");
            }
        }

    }


    private static boolean isNull(String s) {
        if ("".equals(s)) {
            return true;
        }
        return false;
    }

    private boolean isEquals(String fileName) {
        String[] file = fileName.split("\\.");
        String[] fileType = {"jpg", "png", "psd", "jpeg", "gif"};
        for (String type : fileType) {
            if (type.equalsIgnoreCase(file[file.length - 1])) {
                return true;
            }
        }
        return false;
    }

    private boolean isLarge(List<MultipartFile> images) {
        for (MultipartFile m : images) {
            if (m.getSize() > 5120000) {
                return true;
            }
        }
        return false;
    }

    private boolean isImages(List<MultipartFile> images) {
        for (MultipartFile m : images) {
            if (isEquals(m.getOriginalFilename())) {
                return true;
            }
        }
        return false;
    }

    private boolean imgIsNull(List<MultipartFile> images) {
        for (MultipartFile m : images) {
            if ("".equalsIgnoreCase(m.getOriginalFilename())) {
                return true;
            }
        }
        return false;
    }
}
